<template>
  <view>
    <view>
      <view class="scan-btn" @click="gridRfidClick">
        <view class="icon"></view>
        <view>{{ onOff ? "点击感应RFID标签" : "点击停止感应" }}</view>
      </view>
      <view>initRFIDStr:{{ initRFIDStr }}</view>
      <view>startInventoryStr:{{ startInventoryStr }}</view>

      <view> 扫描得到的结果： </view>
      <view v-for="(item, index) in list" :key="index">
        {{ index }}---{{ item }}
      </view>
    </view>

    <view style="margin-top: 20px">
      <view class="scan-btn" @click="gridClick">
        <view class="icon"></view>
        <view>扫码绑定货架</view>
      </view>
      扫描得到的结果：<text> {{ code }}</text>
    </view>
  </view>
</template>
<script>
import { scan } from "./scan.js";

var urovoPlatformModule = uni.requireNativePlugin("TH-PlatformSDK");

var urovoRFIDModule = uni.requireNativePlugin("TH-PlatformRFID");
export default {
  data() {
    return {
      onOff: true,
      initRFIDStr: "",
      startInventoryStr: "",
      list: [],

      code: null, //扫描数据
    };
  },
  created() {
    console.log(1112);
    // //扫描头
    urovoPlatformModule.setScanMode(0, 0);
    scan.init(this.getScancode);
    //感应RFID
    this.initRFID();
  },
  mounted() {
    //注册广播
    scan.registerReceiver();
    //感应RFID
    urovoRFIDModule.registerCallbackDatas((ret) => {
      if (!this.list.includes(ret.epc)) {
        this.list.push(ret.epc);
      }
    });
  },
  beforeDestroy() {
    //销毁广播
    scan.unregisterReceiver();
  },
  methods: {
    // 扫描头
    gridClick() {
      urovoPlatformModule.startScan();
    },
    getScancode(code) {
      this.code = JSON.stringify(code);
    },

    //rfid
    gridRfidClick() {
      if (this.onOff) {
        this.list = [];
        this.startInventory();
        this.onOff = !this.onOff;
      } else {
        this.stopInventory();
        this.onOff = !this.onOff;
      }
    },

    initRFID() {
      urovoRFIDModule.initRFID("/dev/ttyHSL0", "115200", (ret) => {
        this.initRFIDStr = JSON.stringify(ret);
      });
    },
    startInventory() {
      urovoRFIDModule.startInventory(0, 0, 0, 1, (ret) => {
        this.startInventoryStr = JSON.stringify(ret);
      });
    },
    stopInventory() {
      urovoRFIDModule.stopInventory();
    },
  },
};
</script>
<style>
.scan-btn {
  background: #3498ff;
  border-radius: 8px;
  font-size: 14px;
  color: #fff;
  text-align: center;
  padding: 27px 0 28px;
  margin-bottom: 10px;
}
.icon {
  width: 34px;
  height: 34px;
  margin: 0 auto;
  background-size: 34px 34px;
  margin-bottom: 5px;
}
</style>